﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EBEInventory_DAL;

public partial class Inventory_Items_Management_ListItems : System.Web.UI.Page
{
    InventoryEntities ctx = null;



    protected void Page_Load(object sender, EventArgs e)
    {
        ctx = InitConnection.Initialize();
    }

    protected void Page_PreRender(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            
            Dictionary<string, string> subNodes = CatOps.GetSubCategories(0, ctx);
            Hidden1.Value = "";

            foreach (string node in subNodes.Keys)
            {
                TreeNode tn = new TreeNode(subNodes[node], node);
                
                TreeVu_Cats.Nodes[0].ChildNodes.Add(tn);
                tn.Collapse();
            }

            //rootNode.Collapse();

        }

        string s =Hidden1.Value;
        Page.DataBind();
    }

    protected void TreeVu_Cats_TreeNodePopulate(object sender, TreeNodeEventArgs e)
    {
        // not selected index changed
        //if (ctx != null)
        //{

        //    Dictionary<string, string> subNodes = CatOps.GetSubCategories(e.Node.Value, ctx);



        //    foreach (string node in subNodes.Keys)
        //    {
        //        TreeNode tn = new TreeNode(subNodes[node], node);
        //        tn.PopulateOnDemand = true;
        //        tn.SelectAction = TreeNodeSelectAction.Select;
        //        e.Node.ChildNodes.Add(tn);
        //    }
        //}
        //Page.DataBind();
    }

    protected void TreeVu_Cats_SelectedNodeChanged(object sender, EventArgs e)
    {
        if (TreeVu_Cats.SelectedNode == null) return;

        category selCat = new category();
        selCat.CategoryID = Convert.ToInt32(TreeVu_Cats.SelectedNode.Value);
        selCat.CategoryName = TreeVu_Cats.SelectedNode.Text;
        Hidden1.Value = TreeVu_Cats.SelectedNode.Value;
       // Session["SelectedCategory"] = selCat;

        if (TreeVu_Cats.SelectedNode.ChildNodes.Count == 0)
        {
            Dictionary<string, string> subNodes = CatOps.GetSubCategories(Convert.ToInt32(TreeVu_Cats.SelectedNode.Value), ctx);

            foreach (string node in subNodes.Keys)
            {
                TreeNode tn = new TreeNode(subNodes[node], node);
                tn.PopulateOnDemand = true;
                tn.SelectAction = TreeNodeSelectAction.Select;
                TreeVu_Cats.SelectedNode.ChildNodes.Add(tn);
            }

            TreeVu_Cats.SelectedNode.Expand();

           // if (TreeVu_Cats.SelectedNode.ChildNodes.Count == 0)
           // {
                //GridItems.DataSource = CatOps.GetCategoryItems(TreeVu_Cats.SelectedNode.Value,ctx);
                //GridItems.DataBind();
           // }
        }

        GridItems.DataSource = CatOps.GetCategoryItems(Convert.ToInt32(TreeVu_Cats.SelectedNode.Value), ctx);
        GridItems.DataBind();

    }
}